package at.ltasat.util;

public final class ExtendedMath {

	public static double pow(final double value, int n) {
		assert n >= 0;
		double result = 1;
		while (n > 0) {
			result *= value;
			n--;
		}
		return result;
	}

	public static double nrt(final double value, final int n) {
		return nrt(value, n, 0.001);
	}

	public static double nrt(final double value, final int n, final double epsilon) {
		assert value > 0;
		assert n > 0;
		assert epsilon > 0;
		double lower = 0;
		double upper = Math.max(1, value);
		double result = lower, rValue = result;
		do {
			if (rValue > value) {
				upper = result;
			} else {
				lower = result;
			}
			result = lower + (upper - lower) / 2;
			rValue = pow(result, n);
		} while (Math.abs(value - rValue) >= epsilon);
		return result;
	}

	private ExtendedMath() {
		// nothing to do
	}

}
